<template>
    <div class="main-con editMallProductClassify">
        <!-- 顶部导航 -->
        <div class="breadcrumb-con">
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/index/mallProductClassify' }">商城商品分类</el-breadcrumb-item>
                <el-breadcrumb-item>{{ editText }}商城商品分类</el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <!-- 表单信息 -->
        <div class="form-con">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="121px" class="demo-ruleForm">
                <h2 class="item-title">
                    商城商品分类信息
                </h2>
                <div class="form-item-con">
                    <el-form-item label="分类名称" prop="categoryName">
                        <div class="form-item-input">
                            <el-input size="small" v-model="ruleForm.categoryName" placeholder="请输入分类名称"></el-input>
                        </div>
                    </el-form-item>
                    <el-form-item label="分类排序" prop="sort">
                        <div class="form-item-input">
                            <el-input size="small" v-model="ruleForm.sort" placeholder="请输入分类排序"></el-input>
                        </div>
                    </el-form-item>
                    <el-form-item label="分类内商品排序" required>
                        <el-radio-group v-model="ruleForm.productSortType">
                            <div style="margin-top: 12px;">
                                <div><el-radio :label="0">按销量从高到低</el-radio></div>
                                <div><el-radio :label="1">按添加时间从后到先</el-radio></div>
                                <div><el-radio :label="2">按添加时间从先到后</el-radio></div>
                            </div>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="是否展示分类" required>
                        <el-switch v-model="ruleForm.showOnClient" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
                    </el-form-item>
                </div>
                <div class="submit-btn-con">
                    <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
                    <el-button @click="handleBack">取消</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import {
    addMallProductClassifyInfo,
    updateMallProductClassifyInfo
} from "@/common/service/productManagement/mallProductClassify.js";
export default {
    name: "editMallProductClassify",
    data() {
        return {
            editType: "", //操作类型  add=添加  update=修改
            editText: "", //操作文字
            editId: "", //操作的id
            editData: '',//操作的数据
            //表单信息
            ruleForm: {
                categoryName: "",
                sort: "999",
                productSortType: 0,
                showOnClient: true
            },
            rules: {
               categoryName: [{
                    required: true,
                    message: '请输入分类名称',
                    trigger: 'blur'
                }, ],
                sort: [{
                    required: true,
                    message: '请输入分类排序',
                    trigger: 'blur'
                }, ]
            },
        };
    },
    created() {
        this.editType = this.$route.query.editType;
        this.editText = this.$route.query.editText;
        if (this.editType == "update") {
            this.editId = this.$route.query.editId;
            this.editData = JSON.parse(localStorage.getItem('editData'));
            this.ruleForm.id = this.editData.id;
            this.ruleForm.categoryName = this.editData.categoryName;
            this.ruleForm.sort = this.editData.sort;
            this.ruleForm.productSortType = this.editData.productSortType;
            this.ruleForm.showOnClient = this.editData.showOnClient;
        }
    },
    beforeDestroy(){
        //清除本地缓存的编辑数据
        localStorage.removeItem("editData");
    },
    methods: {
        /*-----------------------------新增操作-----------------------------*/
        _addMallProductClassifyInfo() {
            addMallProductClassifyInfo(this.ruleForm)
                .then(res => {
                    this.$message({
                        type: "success",
                        message: "添加成功!"
                    });
                    this.handleBack();
                })
                .catchh(err => {
                    this.$message.error(err.message);
                });
        },

        /*-----------------------------编辑操作-----------------------------*/
        _updateMallProductClassifyInfo() {
            updateMallProductClassifyInfo(this.ruleForm)
                .then(res => {
                    this.$message({
                        type: "success",
                        message: "编辑成功!"
                    });
                    this.handleBack();
                })
                .catchh(err => {
                    this.$message.error(err.message);
                });
        },

        /*-----------------------------保存校验数据操作-----------------------------*/
        // 点击确定
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    if (this.editType == "add") {
                        this._addMallProductClassifyInfo();
                    } else if (this.editType == "update") {
                        this._updateMallProductClassifyInfo();
                    }
                } else {
                    return false;
                }
            });
        },

        /*-----------------------------返回操作-----------------------------*/
        handleBack() {
            this.$router.push({
                path: "/index/mallProductClassify"
            });
        }
    }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>